package com.shijie.controller.erp;

import com.shijie.service.MagazinesService;
import com.shijie.domain.entity.Magazines;
import com.shijie.domain.enums.EnumInterface;
import com.shijie.core.Result;
import com.shijie.core.ResultGenerator;
import com.shijie.core.annotation.Log;
import com.shijie.core.annotation.RequiresPermissions;
import com.shijie.core.constants.PnConstant.BusinessType;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;

/*
 *  @author: smart boy
 *  @date: 2018-08-06
 */
@RestController
@RequestMapping("/erp/magazines")
public class MagazinesController {

    @Resource
	private MagazinesService magazinesService;
    @RequiresPermissions(value={"magazines:list","activity:list","vip:content:list"})
	@GetMapping
	public Result list(@RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer pageSize,
			@RequestParam(required = false) EnumInterface.EntityType entityType,
			@RequestParam(required = false) String q,
			@RequestParam(required = false) Integer status) {
		PageHelper.startPage(page, pageSize);
		List<Magazines> list = magazinesService.getMagazinesListByType(entityType, null, status, q);
		PageInfo<Magazines> pageInfo = new PageInfo<>(list);
		return ResultGenerator.success(pageInfo);
	}

	@RequiresPermissions(value={"magazines:create","magazines:edit","magazines:publish","activity:edit","activity:create","activity:publish","vip:content:edit","vip:content:create"})
	@Log(title = "资讯管理", action = BusinessType.INSERT)
	@PostMapping
    public Result add(@RequestBody Magazines magazines) {
        magazinesService.save(magazines);
        return ResultGenerator.success();
	}
	
	@Log(title = "资讯管理", action = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        magazinesService.deleteById(id);
        return ResultGenerator.success();
	}
	@GetMapping("/{id}")
    public Result detail(@PathVariable Integer id) {
        Magazines magazines = magazinesService.selectById(id);
        return ResultGenerator.success(magazines);
	}
}

